import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
    // 登录页面
    {
        path: '/',
        name: 'login',
        component: () => import('../views/login/index')
    },
    {
        path: '/home',
        name: 'test',
        component: () => import('../views/home'),
        children: [
            {
                path: '/home/usermanger',
                name: 'usermanger',
                component: () => import('../views/manger/usermanger')
            },
            {
                path: '/home/menumanger',
                name: 'menumanger',
                component: () => import('../views/manger/menumanger')
            },
            {
                path: '/home/rolemanger',
                name: 'rolemanger',
                component: () => import('../views/manger/rolemanger')
            },
            {
                path: '/home/customerservice',
                name: 'chat',
                component: () => import('../views/service/chatser')
            },
            {
                path: '/home/usercli',
                name: 'usercli',
                component: () => import('../views/service/chat')
            },
            {
                path: '/home/customermange',
                name: 'customermange',
                component: () => import('../views/manger/customermanger')
            },
            {
                path: '/home/goodsmanger',
                name: 'goodsmanger',
                component: () => import('../views/manger/goodsmanger')
            },
            {
                path: '/home/discount',
                name: 'discountmanger',
                component: () => import('../views/manger/discountmanger')
            }
            , {
                path: '/home/ordermanger',
                name: 'ordermanger',
                component: () => import('../views/manger/ordermanger')
            }
            , {
                path: '/home/aftersale',
                name: 'aftersalemanger',
                component: () => import('../views/manger/aftersalemanger')
            }
            , {
                path: '/home/express',
                name: 'expressmanger',
                component: () => import('../views/manger/expressmanger')
            }, {
                path: '/home/userhome',
                name: 'userHome',
                component: () => import('../components/Dashboard.vue')
            }, {
                path: '/home/logmanger',
                name: 'logmanger',
                component: () => import('../views/manger/logmanger.vue')
            }, {
                path: '/home/chat/chatser',
                name: 'chatser',
                component: () => import('../views/chat/chatser.vue')
            }, {
                path: '/home/chat/chatcli',
                name: 'chatcli',
                component: () => import('../views/chat/chat.vue')
            }, {
                path: '/home/newmanger',
                name: 'newmanger',
                component: () => import('../views/manger/advertisement.vue')
            }, {
                path: '/home/customerlocalmap',
                name: 'customerlocalmap',
                component: () => import('../views/echart/CustomerChart.vue')
            }, {
                path: '/home/ordergoods',
                name: 'OrderGoodsChart',
                component: () => import('../views/echart/OrderGoodsChart.vue')
            }, {
                path: '/home/userSallYearMap',
                name: 'userSallYearMap',
                component: () => import('../views/echart/orderMonthMap.vue')
            }

        ]
    }
]

const router = new VueRouter({
    routes
})
export default router

router.beforeEach(((to, from, next) => {
    // 获取到当前的路径信息
    const path = to.path;
    // 若请求为 / 也就是根路径请求，我们就直接放行
    if (path === '/') {
        return next();
    }
    // 否则我们要判断一波token是否为空，若为空则一定未登录让他返回登录页面
    if (localStorage.getItem("userToken")) {
        return next();
    }
    // 否则让他跳转为登录页面
    return next("/")
}))
